//
//  PCollectViewController.m
//  Tiger
//
//  Created by 龚 杰 on 12-7-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PCollectViewController.h"
#import "ASIHTTPRequest.h"
#import "SwordDetailViewController.h"
#import "SVideoListViewController.h"
@interface PCollectViewController ()

@end

@implementation PCollectViewController
@synthesize m_PCollectDateArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    if (self = [super init])
    {
        self.m_PCollectDateArray = [NSMutableArray arrayWithObjects:nil];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = YES;
        
    }
	// Do any additional setup after loading the view.
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"]];
	self.navigationController.navigationBar.tintColor =  [UIColor colorWithRed:33.0/255 green:32.0/255 blue:32.0/255 alpha:1.0];
    
	// Do any additional setup after loading the view.
    
    m_PCollectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 
                                                                self.view.frame.size.height-90) style:UITableViewStylePlain];
    [m_PCollectTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"screen_bg"]]];
    [m_PCollectTableView setDelegate:self];
    [m_PCollectTableView setDataSource:self];
    m_PCollectTableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:m_PCollectTableView];
    
    ASIHTTPRequest* tSwordListRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.speedtiger.com.cn/ios3/knows_list.html"]];
    [tSwordListRequest setDelegate:self];
    [tSwordListRequest startAsynchronous];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark-
#pragma mark ASIHTTPRequestDelegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",[request responseString]);
    NSData *responseData = [request responseData];
	NSString *responseStr = [[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];

    NSDictionary *tdict = [responseStr JSONValue];
    self.m_PCollectDateArray =  [NSMutableArray arrayWithArray:[tdict objectForKey:@"items"]];
    //m_PCollectDateArray
    NSMutableDictionary* tAddVideo = [NSMutableDictionary dictionary];
    [tAddVideo setValue:@"刀具切削影片" forKey:@"cateName"];
    [m_PCollectDateArray addObject:tAddVideo];
    [m_PCollectTableView reloadData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    UIAlertView* tAlertView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败请稍后再试！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
    [tAlertView show];
    [tAlertView release];
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

	return 55.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    int row  = indexPath.row;
    if (row == [m_PCollectDateArray count]-1) {
        SVideoListViewController* tNextVC = [[SVideoListViewController alloc] init];
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                       initWithTitle:@"刀具百科" 
                                       style:UIBarButtonItemStylePlain 
                                       target:self
                                       action:nil];
        self.navigationItem.backBarButtonItem = backButton;
        [self.navigationController pushViewController:tNextVC animated:YES];
        [tNextVC release];
    }else {
        SwordDetailViewController* tNextVC =[[SwordDetailViewController alloc] init];
        NSDictionary* tRowDic = [m_PCollectDateArray objectAtIndex:indexPath.row];
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                       initWithTitle:@"刀具百科" 
                                       style:UIBarButtonItemStylePlain 
                                       target:self
                                       action:nil];
        self.navigationItem.backBarButtonItem = backButton;
        tNextVC.m_CatgID = [[tRowDic objectForKey:@"cateID"] intValue];
        tNextVC.m_KnowTitle = [tRowDic objectForKey:@"cateName"];
        [self.navigationController pushViewController:tNextVC animated:YES];
        [tNextVC release];
    }
}


#pragma mark UITableViewDataSource
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    int num = [m_PCollectDateArray count];
    if (num > 0) {
        return num;
    }
    return 0;
	
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString* CellIdentifier = @"Cellidentifier";
	
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
		{
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
										   reuseIdentifier:CellIdentifier] autorelease];   
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
         
            UIImageView* cellbg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plistbg"]];
            [cell setBackgroundView:cellbg];
            [cellbg release];
            UIImageView* tCustomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
            [tCustomImgView setImage:[UIImage imageNamed:@"br_nextwhite.png"]];
            cell.accessoryView = tCustomImgView;
            [tCustomImgView release];
        }
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
    NSDictionary* tRowDicData =[m_PCollectDateArray objectAtIndex:indexPath.row];
		cell.textLabel.text = [tRowDicData objectForKey:@"cateName"];
		
    return cell;
}

- (void)dealloc
{
    [m_PCollectDateArray release];
    [m_PCollectTableView release];
    [super dealloc];
}
@end
